# -*- coding: utf-8 -*-

from server.models import db
from server.models import ObjectId

'''
数据库结构
    { "_id" : , 
      "effective": ,
      "name": ,
      "producer": ,
      "type": ,
      "createDate": ,
      "creator": ,
      "details": ,
      "describe": 
     }
'''


class equipmentDao(object):

    # 初始化变量
    def __init__(self):
        self._collection = db.equipmentTable

    # 查找单项生效数据
    def select_equipment_one(self, condition):
        if not isinstance(condition, dict):
            return None

        # condition['Effective'] = True

        equipment = self._collection.find(condition)

        return equipment

    # 查找所有生效数据
    def select_equipments(self, condition, limit):
        if not isinstance(condition, dict):
            return None

        count = self._collection.find(condition).count()
        if count == 0:
            return count, []

        equipments = []

        for equipmentInfo in self._collection.find(condition):
            equipmentInfo['id'] = str(equipmentInfo['_id'])
            equipmentInfo.pop('_id')
            equipments.append(equipmentInfo)

        return count, equipments[(limit['page'] - 1) * int(limit['limit']):
                                 (limit['page'] - 1) * int(limit['limit']) + limit['limit']]

    # 新建数据
    def add_equipment(self, equipment):
        if not isinstance(equipment, dict):
            return False

        self._collection.insert_one(equipment)
        return True

    # 修改设备数据
    def update_equipment(self, equipment):
        if not isinstance(equipment, dict):
            return False

        updateInfo = self._collection.find_one({"_id": ObjectId(equipment["id"]), "effective": 1})
        if updateInfo is None:
            return False

        updateInfo['effective'] = 0

        self._collection.update_one({"_id": ObjectId(equipment["id"])}, {'$set': updateInfo})

        updateInfo['effective'] = 1
        updateInfo.pop('_id')

        for key in equipment:
            updateInfo[key] = equipment[key]

        self._collection.insert_one(updateInfo)

        return True

    # 失效数据
    def invalid_equipment(self, equipment):
        if not isinstance(equipment, dict):
            return False

        updateInfo = self._collection.find_one({"_id": ObjectId(equipment["id"]), "effective": 1})
        if updateInfo is None:
            return False

        updateInfo['effective'] = 0

        self._collection.update_one({"_id": ObjectId(equipment["id"])}, {'$set': updateInfo})

        return True

    # 激活数据
    def activate_equipment(self, equipment):
        if not isinstance(equipment, dict):
            return False

        updateInfo = self._collection.find_one({"_id": ObjectId(equipment["id"]), "effective": 0})
        if updateInfo is None:
            return False

        updateInfo['effective'] = 1

        self._collection.update_one({"_id": ObjectId(equipment["id"])}, {'$set': updateInfo})

        return True

    # 删除数据
    def delete_equipment(self, equipment):
        if not isinstance(equipment, dict):
            return False

        info = self._collection.find_one({"_id": ObjectId(equipment["id"]), "effective": 0})
        if info is None:
            return False

        self._collection.delete_one({"_id": ObjectId(equipment["id"])})

        return True
